﻿using SuperMap.Realspace;
using SuperMap.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Results;

namespace Ycjw.PipeLine.Map.Basic.Conditions
{
    /****************************************************************************************/
    /*
    功    能:
    作    者: RenZachary
    联系方式: rzcgis@foxmail.com（QQ455745253）
    生成日期: 2021年3月24日
    修改日志: 
    版权说明: @2021 西安云创经纬信息科技有限公司 版权所有 All right reserved.
    注    意: 本内容仅限于云创经纬公司内部使用，禁止用于商业目的。
    */
    /****************************************************************************************/
    public class ConditionPanel : UserControl
    {
        protected ResultWindow m_ResultWindow;
        protected SortedDictionary<string, object> m_BaseConditions = new SortedDictionary<string, object>();
        /// <summary>
        /// 获取所有条件
        /// </summary>
        public SortedDictionary<string, object> BaseConditions
        {
            get
            {
                return m_BaseConditions;
            }
        }
        /// <summary>
        /// 设置参数，子类实现即可，无须调用
        /// </summary>
        /// <returns></returns>
        public virtual SortedDictionary<string, object> SetConditions()
        {
            return m_BaseConditions;
        }
        private Size m_ContainerSize;
        /// <summary>
        /// 设置大小，修改条件面板的大小
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        protected void SetSize(int width,int height)
        {
            this.ParentChanged -= ConditionPanel_ParentChanged;
            this.ParentChanged += ConditionPanel_ParentChanged;
            m_ContainerSize = new Size(width, height);
        }
        /// <summary>
        /// 在父窗口发生变化时候的内容，设置父窗口的大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConditionPanel_ParentChanged(object sender, EventArgs e)
        {
            Control topParent = this;
            while(!(topParent is Form))
            {
                topParent = topParent.Parent;
                if (topParent == null) break;
            }
            if (topParent == null) return;

            topParent.Size = this.m_ContainerSize;
        }

        /// <summary>
        /// 实现此方法以进行计算
        /// </summary>
        /// <param name="dockPanel"></param>
        public virtual object Do(DockPanel dockPanel = null)
        {
            throw new Exception("未实现Ycjw.PipeLine.Map.Tools.ConditionPanel.Do方法");
        }
        /// <summary>
        /// 创建结果窗口，大多数情况下子类无须实现
        /// </summary>
        public virtual void CreateResultWindow()
        {
            var conditionFullName = this.GetType().FullName.Replace(".Conditions.",".Results.") + ",Ycjw.PipeLine.Map";
            var type = Type.GetType(conditionFullName);
            if(type == null && !this.GetType().FullName.Contains("Statistics"))
            {
                Console.WriteLine("··················");
                Console.WriteLine("创建结果面板时未找到结果面板,使用ResultWindowWithDataGridView");
                Console.WriteLine("··················");
                type = Type.GetType("Ycjw.PipeLine.Map.Basic.Results.ResultWindowWithDataGridView,Ycjw.PipeLine.Map.Basic");
            }
            else if (type == null)
            {
                Console.WriteLine("··················");
                Console.WriteLine("创建结果面板时未找到结果面板，使用Statics.ResultWindowWithChartBar");
                Console.WriteLine("··················");
                type = Type.GetType("Ycjw.PipeLine.Map.Basic.Results.ResultWindowWithBarChart,Ycjw.PipeLine.Map.Basic");
            }
            m_ResultWindow = (ResultWindow)Activator.CreateInstance(type);
            if (this.m_BaseConditions.ContainsKey("SceneControl"))
            {
                m_ResultWindow.SetParams("SceneControl", m_BaseConditions["SceneControl"]);
            }
        }
        /// <summary>
        /// 主面板中的DockPanel
        /// </summary>
        protected DockPanel DockPanel
        {
            get
            {
                if (this.BaseConditions.ContainsKey("DockPanel"))
                    return BaseConditions["DockPanel"] as DockPanel;
                else
                    return null;
            }
        }
        /// <summary>
        /// SceneEx控件
        /// </summary>
        protected SceneEx SceneEx
        {
            get
            {
                if (this.BaseConditions.ContainsKey("SceneEx"))
                    return BaseConditions["SceneEx"] as SceneEx;
                else
                    return null;
            }
        }

        protected IMessageInterface MessageInterface
        {
            get
            {
                if (this.BaseConditions.ContainsKey("IMessageInterface"))
                    return BaseConditions["IMessageInterface"] as IMessageInterface;
                else
                    return null;
            }
        }
        /// <summary>
        /// Scene对象
        /// </summary>
        protected Scene Scene
        {
            get
            {
                if (SceneControl != null)
                    return SceneControl.Scene;
                else
                    return null;
            }
        }
        /// <summary>
        /// SceneControl控件
        /// </summary>
        protected SceneControl SceneControl
        {
            get
            {
                if (this.BaseConditions.ContainsKey("SceneControl"))
                    return BaseConditions["SceneControl"] as SceneControl;
                else
                    return null;
            }
        }
        /// <summary>
        /// 是否以模态形式显示查询面板
        /// </summary>
        /// <returns></returns>
        public virtual bool ShowModal()
        {
            return false;
        }
        /// <summary>
        /// 调用此方法实现结果的展示,如果子类中显式的调用，需要返回 ShowByDefault = false;
        /// </summary>
        /// <param name="data"></param>
        /// <param name="ds"></param>
        public virtual void ShowResult(object data, DockPanel dp = null, string resultForm = "执行结果")
        {
            if(m_ResultWindow == null)
            {
                Console.WriteLine("··················");
                Console.WriteLine("显示结果时未找到结果面板");
                Console.WriteLine("··················");
                return;
            }
            bool reShow = false;
            if (m_ResultWindow.IsDisposed)
            {
                reShow = true;
                CreateResultWindow();
            }
            m_ResultWindow.TabText = resultForm;
            m_ResultWindow.Text = resultForm;
            m_ResultWindow.ShowData(data);
            if (!reShow && m_ResultWindow.Visible) return;
            if (dp != null)
            {
                m_ResultWindow.Show(dp, DockState.DockRight);
            }
            else
            {
                m_ResultWindow.Show();
            }
        }
        /// <summary>
        /// 条件输入对话框的标题
        /// </summary>
        public virtual string ConditionPanelName { get; set; }
        /// <summary>
        /// 条件是否有效，如果false，则不会关闭对话框
        /// </summary>
        public bool IsConditionValid { get => isConditionValid; set => isConditionValid = value; }

        private bool isConditionValid = true;
        /// <summary>
        /// 条件是否有效，如果false，则不会关闭对话框，此处可以继承实现，根据不同的条件进行设置
        /// </summary>
        /// <returns></returns>
        public virtual bool ConditionValid()
        {
            return IsConditionValid;
        }

        /// <summary>
        /// 点击OK并关闭对话框后执行
        /// </summary>
        public virtual void AfterCloseWithOK() { }
        /// <summary>
        /// 点击Cancel并关闭对话框后执行
        /// </summary>
        public virtual void AfterCloseWithCancel() { }
        /// <summary>
        /// 关闭对话框后执行
        /// </summary>
        public virtual void AfterClose() { }
    }
}
